<?php
/**
 * Created by PhpStorm.
 * User: liyu
 * Date: 2018/4/3
 * Time: 下午3:31
 */

namespace app\common\controller;


use app\common\exception\StatusCode;
use think\Controller;

abstract class Base extends Controller
{
    /**
     * @param array $data
     * @return array
     */
    public function showJsonReturnCode($data=null)
    {
        header('Access-Control-Allow-Origin: *');
        $status = StatusCode::getError('success');
        $return_data = [
            'code' => $status['code'],
            'msg' => $status['msg'],
        ];
        if (!is_null($data)) {
            $return_data['data'] = $data;
        }
        return json_encode($return_data);
    }

    /**
     * 数据库字段 网页字段转换
     * 标识为数据库字段 值为浏览器提交字段
     * @param $array   标识为数据库字段 值为浏览器提交字段
     * @param bool|false $ugid  是否追加UGID信息
     * @return array
     */
    protected function buildParam($array,$ugid=false)
    {
        $data=[];
        foreach( $array as $item=>$value ){
            $data[$item] = $this->request->param($value);
        }
        if ($ugid && isset($this->$ugid)){
            $data['ugid'] = $this->$ugid;
        }
        return $data;
    }
}